package com.hdwx.weike.dao;

import com.hdwx.weike.Utils.JdbcUtils;
import com.hdwx.weike.beans.User;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by hdwx on 2016/12/24.
 */
public class UserDao extends BaseDao{
    //查询账户密码数据库中是否存在
    public boolean queryByAccountAndPassword(String account,String password){
        String table="t_users";
        String[] col={"account","password"};
        String condition="account='"+account+"' and password='"+password+"'";
        ResultSet set=null;
        try {
            set = query(table, col, condition, null);
            return set.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(set);
        }
        return false;
    }
    //添加一个新用户
    public boolean register(User user){
        String table="t_users";
        String[] cols={"name","account","password","sex","tel","email","work","city","area","school"};
        Object[] values={
                user.getName(),
                user.getAccount(),
                user.getPwd(),
                user.getSex(),
                user.getTel(),
                user.getEmail(),
                user.getWork(),
                user.getCity(),
                user.getArea(),
                user.getSchool()
        };
        return insert(table,cols, values);
    }
    //查询指定字段指定值是否存在
    public boolean queryBy(String colName,String value){
        String table="t_users";
        String[] col={colName};
        String condition=colName+"='"+value+"'";
        ResultSet set=null;
        try {
            set = query(table, col, condition, null);
            return set.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(set);
        }
        return false;
    }


}
